package com.stlm2.dems.service;

import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.GoodSpecification;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.Stages;
import com.stlm2.dems.dao.StagesDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("stagesService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class StagesService extends BaseServiceImpl<Stages> {

	@Resource
	private StagesDao stagesDao;
	@Resource
	private GoodSpecificationService goodSpecificationService;
	@Resource
	private UserService userService;
	@Resource
	private DdmGoodsService ddmGoodsService;

	@Override
	protected BaseDao <Stages> getDao() {
		return stagesDao;
	}

    public ModelMap buiderModel(Stages stages) {
		ModelMap map=new ModelMap();
		User user=userService.getById(stages.getCreateUserId());
		if(stages.getSpecificationId()!=null) {
			GoodSpecification goodSpecification = goodSpecificationService.findEntityById(stages.getSpecificationId());
			map.put("specificationName",goodSpecification!=null?goodSpecification.getPropertiesName():"");
		}
		if(stages.getGoodsId()!=null){
			DdmGoods ddmGoods=ddmGoodsService.findEntityById(stages.getGoodsId());
			map.put("goodsName",ddmGoods.getName());
		}
		map.put("id",stages.getId());
		map.put("createUserName",user.getRealName());
		map.put("createTime", Convert.toFullYMDHMS(stages.getCreateTime()));
		map.put("instalmentAmount",stages.getInstalmentAmount());
		map.put("periods",stages.getPeriods());
		map.put("state",stages.getState()==1?"有效":"无效");
		map.put("prepayment",stages.getPrepayment());
		return map;
    }


	public List<ModelMap> convert(List<Stages> stages) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (stages != null && stages.size() > 0) {
			for (Stages stage : stages) {
				ModelMap map = buiderModel(stage);
				list.add(map);
			}
		}
		return list;
	}
}